package MULE.Model;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.ImageIcon;

/**
 * Represents a Tile that produces resources.
 * 
 * @author jstep
 */
public abstract class ResourceTile extends Tile {

	private boolean muleOn;

	/**
	 * Creates a new ResourceTile tile at the specified coordinates.
	 * 
	 * @param x
	 *            X-coordinate of the tile.
	 * @param y
	 *            Y-coordinate of the tile.
	 */
	public ResourceTile(int x, int y) {
		super(x, y);
	}

	/**
	 * Returns true if the tile can be purchased, false otherwise. Tiles can
	 * only be bought if they are not already owned.
	 * 
	 * @return True if no one owns this tile, false otherwise.
	 */
	public boolean isPurchasable() {
		return this.owner == null;
	}

	/**
	 * Sets the owner of this tile. The tile's border color is changed to match
	 * the new owner's color.
	 * 
	 * @param p
	 *            Player who owns this tile.
	 */
	public void setOwner(Player p) {
		this.owner = p;
		if (p == null) {
			this.setBorderColor(Tile.BORDER_COLOR);
		} else {
			this.setBorderColor(p.getColor());
		}
	}

	/**
	 * Sets whether or not this tile has a mule.
	 * 
	 * @param hasMule
	 *            Whether or not this tile has a mule.
	 */
	public void muleOn(boolean isOn) {
		this.muleOn = isOn;
	}

	/**
	 * Returns true if the tile has a mule.
	 * 
	 * @return True if tile has mule, false otherwise.
	 */
	public boolean hasMule() {
		return muleOn;
	}

	/**
	 * Draws the tile on the given graphics object.
	 * 
	 * @param g
	 *            The canvas on which to draw the tile.
	 */
	public void paintComponent(Graphics g) {
		// Downcast graphics object.
		Graphics2D g2 = (Graphics2D) g;
		// Draw background.
		g2.setColor(this.backgroundColor);
		g2.fillRect(0, 0, WIDTH, HEIGHT);
		// Draw border.
		g2.setColor(this.borderColor);
		g2.setStroke(new BasicStroke(BORDER_WIDTH));
		int borderOffset = BORDER_WIDTH / 2;
		g2.drawRect(borderOffset, borderOffset, WIDTH - BORDER_WIDTH, HEIGHT
				- BORDER_WIDTH);
		// Draw mule.
		if (muleOn) {
			g2.drawRect(0, 0, 10, 10);
		}
		g2.dispose();
	}

	/**
	 * Abstract method. Get this tile's resource capacity.
	 * 
	 * @return This tile's total resource capacity.
	 */
	abstract Inventory getResourceCapacity();

	/**
	 * Returns this tile's current resource production.
	 * 
	 * @return This tile's current resource production.
	 */
	public Inventory getProduction() {
		if (muleOn) {
			Inventory production = new Inventory();
			Inventory capacity = this.getResourceCapacity();
			// int resourceAmnt = capacity.getAmount(muleType);
			// production.add(muleType, resourceAmnt);
			return capacity;
		}
		return new Inventory(0, 0, 0, 0, 0);
	}
}
